﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Launchkit_Backend
{
    public partial class DlgPickIcon : Form
    {
        public DlgPickIcon()
        {
            InitializeComponent();
        }

        private void DlgPickIcon_Load(object sender, EventArgs e)
        {
            Image empty = Properties.Resources.hotdata_small__自定义_;
            List<string> list = Program.IconProvider.IconList;
            List<Image> rows = new List<Image>();
            int rowCount = list.Count / 8;
            if (list.Count % 8 != 0) {
                rowCount++;
            }
            tblIcons.Rows.Add(rowCount);
            int padCount = rowCount * 8;
            for (int i = 0; i < padCount; i++)
            {
                if (i < list.Count)
                {
                    tblIcons[i % 8, i / 8].Value = Program.IconProvider.GetIcon(list[i]);
                    tblIcons[i % 8, i / 8].Tag = list[i];
                }
                else {
                    tblIcons[i % 8, i / 8].Value = empty;
                    tblIcons[i % 8, i / 8].Tag = "";
                }
            }
            
        }

        private void lblChosen_TextChanged(object sender, EventArgs e)
        {
            btnOk.Enabled = lblChosen.Text != "";
            if (lblChosen.Text != "")
            {
                imgPrev.Image = Program.IconProvider.GetIcon(lblChosen.Text);
            }
            else {
                imgPrev.Image = Properties.Resources.hotdata_small__自定义_;
            }
        }

        private void openFileDialog1_FileOk(object sender, CancelEventArgs e)
        {
            lblChosen.Text = openFileDialog1.FileName;

        }

        private void tblIcons_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            DataGridViewCell cell = tblIcons[e.ColumnIndex, e.RowIndex];
            if (cell.Tag != null) {
                if (cell.Tag.ToString() != "") {
                    lblChosen.Text = cell.Tag.ToString() ;
                }
            }
        }

        private void borderlessButton1_Click(object sender, EventArgs e)
        {
            openFileDialog1.ShowDialog();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            DialogResult = DialogResult.Cancel;
        }

        public static string PickIcon(Form container) {
            DlgPickIcon dp = new DlgPickIcon();
            if (dp.ShowDialog(container) == DialogResult.OK) {
                dp.Dispose();
                return dp.lblChosen.Text;
            }
            dp.Dispose();
            return null;
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            DialogResult = DialogResult.OK;
        }
    }
}
